package com.bpc.cs.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.Order;
import org.slf4j.Logger;

import com.bpc.cs.dto.RuleDTO;
import com.bpc.cs.dto.SchemeDTO;
import com.bpc.cs.util.LoggerUtils;

/**
 * @author   phan_tp
 * @datetime Nov 30, 2012 : 5:19:37 PM
 */
public class SchemeDAO extends BaseDAO implements ISchemeDAO {
	Logger logger = LoggerUtils.getLogger();

	/**
	 * Get all ScoringScheme record
	 * @return ArrayList<ScoringSchemeDTO>
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<SchemeDTO> selectAllScheme() {
		logger.debug("selectAllScheme");
		List<SchemeDTO> lstScrSch = getSession().createCriteria(SchemeDTO.class)
												.addOrder(Order.asc("schemeName"))
												.list();
		return lstScrSch;
	}

	/**
	 * Insert Scheme record
	 * @param SchemeDTO schemeDTO
	 * @return boolean TRUE : Insert successfully
	 * 				   FALSE: Insert failed
	 */
	@Override
	public boolean insertScheme(SchemeDTO schemeDTO) {
		String sqlQuery = "INSERT INTO \"SCORING_SCHEME\" (\"SS_ID\", \"SS_NAME\") VALUES ((SELECT NEXTVAL('scheme_id_seq')), ?)";
		getSession().createSQLQuery(sqlQuery).setParameter(0, schemeDTO.getSchemeName()).executeUpdate();
//		getSession().save(schemeDTO);
		return true;
	}

	/**
	 * Update Scheme record
	 * @param SchemeDTO schemeDTO
	 * @return boolean TRUE : Update successfully
	 * 				   FALSE: Update failed
	 */
	public boolean updateScheme(SchemeDTO schemeDTO) {
		getSession().update(schemeDTO);
		return true;
	}

	/**
	 * Delete Scheme record
	 * @param int schemeID
	 * @return boolean TRUE : Delete successfully
	 * 				   FALSE: Delete failed
	 */
	public boolean deleteScheme(int schemeID) {
		getSession().delete((SchemeDTO)getSession().get(SchemeDTO.class, schemeID));
		return true;
	}

	/**
	 * Select list rule by SchemeID
	 * @param int schemeID
	 * @return List<RuleDTO>
	 */
	@Override
	public List<RuleDTO> selectRuleBySchemeByID(int schemeID) {
		// Loadd scheme
		SchemeDTO schemeDTO = (SchemeDTO)getSession().get(SchemeDTO.class, schemeID);

		// List contains rules
		List<RuleDTO> listRuleDTO = new ArrayList<RuleDTO>();
		Iterator<RuleDTO> iRuleDTO = schemeDTO.getRuleDTO().iterator();

		// Add all rule to list
		while (iRuleDTO.hasNext()) {
			RuleDTO ruleDTO = iRuleDTO.next();
			listRuleDTO.add(ruleDTO);
		}
		return listRuleDTO;
	}

	/**
	 * Count the number of rule of a scheme
	 * @param int schemeID
	 * @return int
	 */
	@Override
	public int countRuleOfSchemeID(int schemeID) {
		return ((SchemeDTO)getSession().get(SchemeDTO.class, schemeID)).getRuleDTO().size();
	}

	/**
	 * Check schemeName is duplicated or not
	 * @param String schemeName
	 * @return boolean TRUE : schemeName is duplicated
	 * 				   FALSE: schemeName isn't duplicated
	 */
	public boolean isDuplicatedSchemeName(String schemeName) {
		return 0 < ((Number)getSession().createQuery("SELECT COUNT(*) FROM SchemeDTO WHERE schemeName = ?")
										.setParameter(0, schemeName)
										.uniqueResult())
										.intValue();
	}
}
